<?php
namespace app\api\controller\user;

use app\api\controller\Controller;
use app\common\model\user\Receive;
use app\store\model\user\UserLabel;
use app\api\model\Order as OrderModel;
use app\common\model\user\LabelReward;

class Label extends Controller {

    public function getUserLabel(){
        // 当前用户信息
        $user = $this->getUser(false);
        $user_label_id=$user->user_label_id;
        $model=new UserLabel();
        $d=$model->getApiOne($user_label_id);
        $data['current_info']['label_id']=$d['label_id'];
        $data['current_info']['name']=$d['name'];
        $data['current_info']['label_img']=$d['image']['file_path'];

        $data['all_info']=[];
        $all_info=$model->getApiAll($user_label_id);
        foreach($all_info as $k=>$v){
            $data['all_info'][$k]['label_id']=$v['label_id'];
            $data['all_info'][$k]['name']=$v['name'];
            $data['all_info'][$k]['pay_amount']=$v['pay_amount'];
            $data['all_info'][$k]['label_img']=$v['image']['file_path'];
            if($v['label_id']==$user_label_id){
                $data['all_info'][$k]['is_mine']=1;
            } else {
                $data['all_info'][$k]['is_mine']=0;
            }
            if($d['weight']>=$v['weight']){
                $data['all_info'][$k]['reached']=1;
            } else {
                $data['all_info'][$k]['reached']=0;
            }
        }

        $nextData=$model->getApiNext($user_label_id);//当前级别的下一级别数据
        $orderModel=new OrderModel();
        $total_price=$orderModel->getAllOrderPrice($user->user_id);
        if($nextData){
            $data['current_info']['less_price']=$nextData['pay_amount']-$total_price;
            if($data['current_info']['less_price']<0){
                $data['current_info']['less_price']=0;
            }
            $data['current_info']['next_label_name']=$nextData['name'];
            $data['current_info']['next_is_exist']=1;
        } else {
            $data['current_info']['next_is_exist']=0;
        }

        $data['label_info']=$model->getApiHighLow();

        $rewardModel=new LabelReward();
        $receiveModel=new Receive();
        $rewardData=$rewardModel->getApiList();

        foreach($rewardData as $k=>$v){
            $data['reward_info'][$k]['reward_id']=$v['reward_id'];

            $data['reward_info'][$k]['name']=$v['name'];
            $data['reward_info'][$k]['name_']=mb_substr($v['name'],0,7,'utf-8');
            if(strlen($v['name'])>14){
                $data['reward_info'][$k]['name_'].='...';
            }

            $receiveExistId=$receiveModel->getReceived($v['reward_id'],$user->user_id);
            $data['reward_info'][$k]['icon_img']=$v['image']['file_path'];
            $data['reward_info'][$k]['pay_amount']=$v['label']['pay_amount'];
            if($v['label']['weight']>$d['weight']){
                $data['reward_info'][$k]['lock']=1;

                $pay_amount=UserLabel::where(['label_id'=>$v['label_id']])->value('pay_amount');
                $label_name=UserLabel::where(['label_id'=>$v['label_id']])->value('name');
                $data['reward_info'][$k]['reward_less_price']=$pay_amount-$total_price;
                $data['reward_info'][$k]['label_name']=$label_name;
            } else {
                $data['reward_info'][$k]['lock']=0;
            }

            if($receiveExistId){
                $data['reward_info'][$k]['received']=1;
            }else{
                $data['reward_info'][$k]['received']=0;
            }

        }
        //dump($rewardData);

        if($data){
            return $this->renderSuccess($data, '数据获取成功');
        } else {
            return $this->renderError('数据获取失败');
        }

    }

    public function rewardReceive(){
        // 当前用户信息
        $user = $this->getUser(false);
        $reward_id=input('reward_id',0,'intval');
        if(!$reward_id){
            return $this->renderError('缺少特权ID');
        }
        $realname=input('realname','','trim');
        if(!$realname){
            return $this->renderError('请填写姓名');
        }
        $telephone=input('telephone','','trim');
        if(!$telephone){
            return $this->renderError('请填写电话');
        }
        $address=input('address','','trim');
        if(!$address){
            return $this->renderError('请填写地址');
        }
        $model=new Receive();

        $isExistId=$model->getReceived($reward_id,$user->user_id);
        if($isExistId){
            return $this->renderError('您已领取过');
        }
        $rewardModel=new LabelReward();
        $saveData=[];
        $saveData['reward_id']=$reward_id;
        $saveData['realname']=$realname;
        $saveData['telephone']=$telephone;
        $saveData['address']=$address;
        $saveData['label_id']=$rewardModel->getApiLabelId($reward_id);
        $saveData['user_id']=$user->user_id;
        $saveData['receive_time']=time();

        $res=$model->add($saveData);
        if($res){
            return $this->renderSuccess([], '领取成功');
        } else {
            return $this->renderError('领取失败');
        }
    }

}